package cn.wolfcode.util;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;


// MyBatis 工具类,用 abstract修饰  (防止调用者,创建对象调用方法)
//全是静态方法
public abstract class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory;

    //为什么利用SQLSessionFactoryBuilder;类的方法来创建SqlSessionFactory对象
    static {
        // 找文件,
        // 创建SqlSession对象为sqlSessionFactory字段赋值;
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder()
                    .build(Resources.getResourceAsReader("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession (){
        return sqlSessionFactory.openSession();
    }

}
